﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MicroFractal.iTunesBrowser.Core.Device;
using MicroFractal.iTunesBrowser.Business;

namespace MicroFractal.iTunesBrowser
{
    /// <summary>
    /// Interaction logic for FrmDownloadProgress.xaml
    /// </summary>
    public partial class FrmDownloadProgress : Window
    {

        #region Constructors
        
        public FrmDownloadProgress()
        {
            InitializeComponent();
        }

        #endregion
        
        #region Attributes

        IDevice _device;
        bool _downloadFinish = false;
        bool _downloadCanceled = false;

        #endregion

        #region Public Methods

        /// <summary>
        /// Start the download of the specified items from the specified device into the specified folder.
        /// The process will open a modal dialog that shows the progress of the download.
        /// </summary>
        /// <param name="device">Device that contains the items</param>
        /// <param name="items">Items to download</param>
        /// <param name="downloadPath">Destination path for the downloaded items</param>
        public void StartDownload(IDevice device, List<IDownloadableObject> items, string downloadPath)
        {
            if (device == null)
                throw new ArgumentNullException("device");
            if (items == null || items.Count == 0)
                throw new ArgumentException("Theres nothing to download!");
            
            _device = device;

            device.DownloadCompleted += new DownloadProgressEventHandler(device_DownloadCompleted);
            device.DownloadProgressChange += new DownloadProgressEventHandler(device_DownloadProgressChange);
            device.AsyncDownload(items, downloadPath, true);

            this.ShowDialog();

        }

        #endregion

        #region Private Events

        void device_DownloadProgressChange(object sender, MicroFractal.iTunesBrowser.Core.Device.DownloadProgressEventArgs e)
        {
            downloadingProgress.Value = e.Progress;
            lblDownloadingSong.Text = string.Format("{0}", e.NextDownloadingSong == null ? e.DownloadedSong.Title : e.NextDownloadingSong.Title);
        }

        void device_DownloadCompleted(object sender, MicroFractal.iTunesBrowser.Core.Device.DownloadProgressEventArgs e)
        {
            _downloadFinish = true;
            if (!_downloadCanceled)
            {
                downloadingProgress.Value = 100;
                lblDownloadingSong.Text = "Download Finished Sucessfully!";
            }
            else
            {
                downloadingProgress.Value = 0;
                lblDownloadingSong.Text = "Download Canceled!";
                Cursor = Cursors.Arrow;
                cmdCancel.IsEnabled = true;
            }
            cmdCancel.Content = "Close";
        }

        private void cmdCancel_Click(object sender, RoutedEventArgs e)
        {
            if (_downloadFinish)
                this.Close();
            else
            {
                _downloadCanceled = true;
                cmdCancel.IsEnabled = false;
                Cursor = Cursors.Wait;
                _device.CancelDownload();
            }
        }

        #endregion

    }
}
